from django.core.management.base import BaseCommand
import tsl_backend.users.models as users


class Command(BaseCommand):
    help = "Create Roles for user"

    def add_arguments(self, parser):
        parser.add_argument("--role", type=str)
        parser.add_argument("--username", type=str)

    
    def handle(self, *args, **options):
        print(options)
        username=options["username"]
        if options["role"]=="all":
            user_obj = users.User.objects.get(username=username)
            
            role_objs = users.Role.objects.all()
            department_objs = users.Department.objects.all()
            for department_obj in department_objs:
                for role_obj in role_objs:
                    account, created_account = users.Account.objects.get_or_create(
                    user_code="XXXX",
                    django_user=user_obj,
                    department=department_obj,
                    role=role_obj,
                    defaults={"default_printer": users.Printer.objects.first(),
                            "enable":True},
                    )
                    print(f"Create account {account} for {role_obj} for user {user_obj} in department {department_obj}: {created_account}")


            self.stdout.write(
                self.style.SUCCESS('Successfully to create roles for user')
            )

    def set_worker_permission(self, role_perm_obj):
        print("Set worker permission")
        role_perm_obj.menu_massscan_perm = True
        role_perm_obj.menu_orderview_perm = True
        role_perm_obj.menu_printlabel_perm = False
        role_perm_obj.report_worker_storage_perm = True
        role_perm_obj.report_worker_transaction_perm = True
        role_perm_obj.report_worker_handset_perm = True
        role_perm_obj.report_qc_handset_perm = False
        role_perm_obj.report_qc_transaction_perm = False
        role_perm_obj.report_printstatus_perm = False
        role_perm_obj.report_leadtime_perm = False
        role_perm_obj.menu_updatert_perm = False
        role_perm_obj.save()

    
    def set_qc_permission(self, role_perm_obj):
        print("Set QC/leader/stoneQC permission")
        role_perm_obj.menu_massscan_perm = True
        role_perm_obj.menu_orderview_perm = True
        role_perm_obj.menu_printlabel_perm = False
        role_perm_obj.report_worker_storage_perm = False
        role_perm_obj.report_worker_transaction_perm = False
        role_perm_obj.report_worker_handset_perm = False
        role_perm_obj.report_qc_handset_perm = True
        role_perm_obj.report_qc_transaction_perm = True
        role_perm_obj.report_printstatus_perm = True
        role_perm_obj.report_leadtime_perm = False
        role_perm_obj.menu_updatert_perm = False
        role_perm_obj.save()

    
    def set_others_permission(self, role_perm_obj):
        print("Set others permission")
        role_perm_obj.menu_massscan_perm = False
        role_perm_obj.menu_orderview_perm = True
        role_perm_obj.menu_printlabel_perm = True
        role_perm_obj.report_worker_storage_perm = False
        role_perm_obj.report_worker_transaction_perm = False
        role_perm_obj.report_worker_handset_perm = False
        role_perm_obj.report_qc_handset_perm = True
        role_perm_obj.report_qc_transaction_perm = True
        role_perm_obj.report_printstatus_perm = True
        role_perm_obj.report_leadtime_perm = True
        role_perm_obj.menu_updatert_perm = True
        role_perm_obj.save()


